#include <pcap.h>

#include "pcap_task.h"
#include "pcap_process.h"
#include "uthash.h"
#include "use_hash.h"
#include "task_manager.h"
#include "task.h"
#include "log.h"
#include "buff_queue.h"

#include<stdio.h>

#include<mysql/mysql.h>


unsigned long raw_packet_count = 0;
unsigned long ipv4_packet_count = 0;
record_t *head=NULL;

MYSQL mysql;
struct task_manager *Task_manager=NULL;
struct core_conf *Core_conf=NULL;

struct buff_queue * Buffer_queue=NULL;

extern void * dispatch();


void main()
{
	//init the Buff_queue
	Buffer_queue=(struct buff_queue*)malloc(sizeof(struct buff_queue));

	Buffer_queue->count=0;
	
//	buff_queue_init(Buffer_queue);	
	
	//init the center value task_manager
	Task_manager=task_manager_create();
	//init the core_conf
	Core_conf=(struct core_conf *)malloc(sizeof(struct core_conf));

	Core_conf->core_conf_file=NULL;
        Core_conf->pcap_mode=1;


        Core_conf->pcap_file=NULL;
        Core_conf->pcap_interface="eth0";
        Core_conf->pcap_handle=NULL;
        Core_conf->pcap_datalink_type=0;


        Core_conf->max_id=5000000;
        Core_conf->mode=1;

        Core_conf->usleep_time=10;

        Core_conf->use_mm=1;
        Core_conf->mm_limit=2147483647;

        Core_conf->use_mail=1;

        Core_conf->to="2412546652@qq.com";
	
	//init the task_manager and give it initial value

	task_manager_init(Task_manager,Core_conf);
	task_manager_finish(Task_manager);
	
	//add task socket to the task_manager
	
	register_plugin_task(Task_manager,"socket_send",dispatch);
	
	start_task(Task_manager);



	printf("begin pcap_loop.");

	pcap_t *pcap_handle = pcap_init(0,"/home/mark/part_packet/pcap_offline/packet","eth0");

	if (pcap_handle == NULL) {
		printf("pcap_handle is NULL.");
		/* TODO:kill task */
		return;
	}

        mysql_init(&mysql);

        if(mysql_real_connect(&mysql,"localhost","root","123456","sklccdpi",0,NULL,0))
        {
                printf("ok\n");
        }
        else
        {
                printf("ERROR");
        }


	int res;
	res = mysql_query(&mysql,"create table pcapTableInfo(id int, tableName char(128))");
	if (res) {
		printf("sql_query error:%s\n",mysql_error(&mysql));
	}

	pcap_loop(pcap_handle, -1, &pcap_task_packet_process, NULL);

	/* NOT REACHED HERE */
	return;
}
